<?php

namespace App\Http\Controllers\Webdisk;

use App\Http\Controllers\Controller;
use App\Webdisk\Model\Playlist;
use App\Webdisk\Model\PlaylistInfo;
use App\Webdisk\Model\User;
use Illuminate\Http\Request;

use function App\Webdisk\makeRes;

class MediaController extends Controller
{



    public function __construct(Request $req)
    {
        $this->middleware('checkPlaylistOwner')->only(['addToList', 'removeFromList']);
    }

    //
    function getPubliclist(Request $req){
        return makeRes(1,Playlist::where('type', '=', PLAYLIST_TYPE_PUBLIC)->get()->toArray());
    }

    function addPlaylist(Request $req){
        $req->validate([
            'name' => ['required'],
            'introduce' => ['required'],
            'type' => ['required']
        ]);
        $p_list = new Playlist();
        $p_list->name = $req->input('name');
        $p_list->introduce = $req->input('introduce');
        $p_list->img_url = $req->input('img_url');
        $p_list->type = $req->input('type') == PLAYLIST_TYPE_PUBLIC ? PLAYLIST_TYPE_PUBLIC : PLAYLIST_TYPE_PRIVATE;
        $p_list->owner_id = $req->attributes->get('user')->id;
        $p_list->save();
        return makeRes();
    }

    function getMyList(Request $req){
        return makeRes(1, Playlist::where('owner_id', '=', $req->attributes->get('user')->id)->get());
    }

    function removeFromList(Request $req){
        $req->validate([
            'list_id' => ['required'],
            'num' => ['required']
        ]);
        $list_id = $req->input('list_id');
        $num = (int)$req->input('num');
        $res = PlaylistInfo::where('list_id',$list_id)
                            ->where('num',$num)
                            ->first();
        if ( $res == null ){
            return makeRes(404,null,"不存在的歌曲");
        }else{
            $res = PlaylistInfo::where('list_id',$list_id)->where("num",$num)->delete();
            return makeRes(1, $res);
        }
    }

    function addToList(Request $req){
        $req->validate([
            'list_id' => ['required'],
            'name' => ['required'],
            'path' => ['required']
        ]);
        $res_root = env("RES_ROOT");
        $file_path = $res_root . '/' . $req->input('path');
        if ( file_exists($file_path) && is_file($file_path) ){
            $max_num = PlaylistInfo::where('list_id', '=', $req->input('list_id'))->max('num');
            if ( $max_num == null ){
                $max_num = 0;
            }
            PlaylistInfo::insert([
                'list_id' => $req->input('list_id'),
                'name' => $req->input('name'),
                'path' => $req->input('path'),
                'num' => ++$max_num
            ]);
            return makeRes(1, $max_num);
        }else{
            return makeRes(-3, $file_path, '文件不存在或为文件夹');
        }
    }

    function getListContent(Request $req){
        $req->validate([
            'list_id' => ['required']
        ]);
        $list_info = Playlist::where(['id' => $req->input('list_id')])->first();
        try {
            if ( $list_info['type'] != PLAYLIST_TYPE_PUBLIC && $list_info['owner_id'] != User::getByToken($_COOKIE['utoken'])->id ){
                return makeRes(-1,null,'无权限');
            }
        } catch (\Throwable $th) {
            return makeRes(-1,null,'无权限');
        }
        $creater = User::find($list_info['owner_id'])['user'];
        $list_info['creater'] = $creater;
        $list = PlaylistInfo::getList($list_info['id']);
        $res = [
            'list_info' => $list_info,
            'list_content' => $list
        ];
        return makeRes(1, $res);
    }

}
